<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mindex extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getRestaurantLabelDb($time)
    {
        $timeupdate = $this->db->query("SELECT content FROM options WHERE tags = 'time_update_restaurant'")->row_object();
        if($time < $timeupdate->content)
        {
            $sql = "SELECT id,name,image,logo,share_facebook_url,hotline FROM restaurant ORDER BY orderby ASC";
            $data = $this->db->query($sql)->result_array();
            
            $arr = array();
            foreach($data AS $item)
            {
                $sql_zone = "SELECT id,title FROM zone ORDER BY id ASC";
                $zone = $this->db->query($sql_zone)->result_array();
                $arr_zone = array();
                foreach($zone AS $itemz)
                {
                    $sql_address = "SELECT ra.address,ra.phone,ra.latitude,ra.longitude FROM restaurant_address AS ra LEFT JOIN zone AS z ON z.id = ra.id_zone WHERE ra.restaurant_id = ".$item['id']." AND z.id = ".$itemz['id']." ORDER BY ra.id ASC";
                    $address = $this->db->query($sql_address)->result_array();
                    $itemz['list'] = $address;
                    $arr_zone[] = $itemz;
                }
                $item['address'] = $arr_zone;
                
                $arr['id_'.$item['id']] = $item;
            }
            
            $arr['time_update'] = $timeupdate->content;
            return (object)array('status' => 200, 'data' => (array)$arr);
        }else{
            return (object)array('status' => 201, 'data' => '');
        }
    }
    
    public function getStaticDataDb($time)
    {
        $timeupdate = $this->db->query("SELECT content FROM options WHERE tags = 'time_update_api_static'")->row_object();
        if($time < $timeupdate->content)
        {
            $slides = $this->db->query("SELECT slides FROM slides LIMIT 1")->row_array();
            $delivery = $this->db->query("SELECT features,title,content FROM home_delivery LIMIT 1")->row_array();
            $faq = $this->db->query("SELECT question,answer FROM faq ORDER BY id DESC")->result_object();
            
            $logo_gpeople = $this->db->query("SELECT content FROM options WHERE tags = 'logo_gpeople'")->row_object();
            $fanpage = $this->db->query("SELECT content FROM options WHERE tags = 'fanpage'")->row_object();
            
            $slide_arr = array();
            foreach(json_decode($slides['slides']) AS $item)
            {
                $slide_arr[] = $item;
            }
            
            $arr = array(
                'slides' => $slide_arr,
                'delivery' => $delivery,
                'faq' => $faq,
                'logo_gpeople' => $logo_gpeople->content,
                'fanpage' => $fanpage->content,
                'time_update' => $timeupdate->content
            );
            
            return (object)array('status' => 200, 'data' => (object)$arr);
        }else{
            return (object)array('status' => 201, 'data' => '');
        }
    }
}